package fr.toto.settings.defaultPanels;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.nio.file.Path;
import java.nio.file.Paths;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class JDirectory extends JPanel implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = -875291147700955640L;
	private String name = " ";
	private Path path;
	JFileChooser fc;
	private JTextField textField;

	/**
	 * Create the applet.
	 */
	public JDirectory(String title, Path selectedPath) {

		this.name = title;
		if (selectedPath != null)
			this.path = selectedPath;
		else
			this.path = Paths.get("");
		
		this.setLayout(new BorderLayout(1, 1));
		this.setSize(new Dimension(500, 45));
		this.setMinimumSize(new Dimension(50,40));

		JLabel lblTitle = new JLabel(this.name);
		this.add(lblTitle, BorderLayout.NORTH);

		JButton btnNewButton = new JButton("Select");
		add(btnNewButton, BorderLayout.EAST);
		btnNewButton.setActionCommand("ADD");
		btnNewButton.addActionListener(this);

		textField = new JTextField();
		textField.setEditable(false);
		add(textField, BorderLayout.CENTER);
		textField.setColumns(1);
		textField.setText(this.path.toString());
		textField.setMaximumSize(new Dimension(500, 20));
		textField.setMinimumSize(new Dimension(20, 20));
		
		JPanel panel = new JPanel();
		add(panel, BorderLayout.SOUTH);

	}

	public JDirectory(String title) {
		this(title, Paths.get(" "));
	}

	public JDirectory() {
		this("Dossier :", Paths.get(" "));
	}

	public void actionPerformed(ActionEvent arg0) {
		if (arg0.getActionCommand().equalsIgnoreCase("ADD")) {

			fc = new JFileChooser();
			fc.setDialogTitle("Selection du dossier");
			fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			fc.setCurrentDirectory(path.toFile());
			//
			// disable the "All files" option.
			//
			fc.setAcceptAllFileFilterUsed(false);
			//
			if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
				this.path = fc.getSelectedFile().toPath();
				textField.setText(this.path.toString());
			}
		} 
	}

	public Path getPath() {
		return path;

	}

}
